﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace THO7AlgoritmTimerApplication {
    class yellow_filter : VisionAlgorithm {
        int marge = 0;
        public yellow_filter(String name) : base(name) {}
        public override Bitmap DoAlgorithm(Bitmap sourceImage) {
            this.marge = getMarge();
            Bitmap dest = new Bitmap(sourceImage.Width, sourceImage.Height);
            BitmapData bmpdata = sourceImage.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            BitmapData bmpdata1 = dest.LockBits(new Rectangle(0, 0, sourceImage.Width, sourceImage.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
            IntPtr ptr = bmpdata.Scan0;
            IntPtr ptr1 = bmpdata1.Scan0;
            int regellengte = Math.Abs(bmpdata.Stride);
            int bytes = regellengte * sourceImage.Height;
            byte[] rgbValues = new byte[bytes+1];
            byte[] output = new byte[bytes+1];
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            for (int regelnummer = 0; regelnummer < sourceImage.Height; regelnummer++) {
                for (int convolutie = 0; convolutie < regellengte; convolutie += 3) {
                    int pixel = regelnummer * regellengte + convolutie;
                    if ((((rgbValues[pixel + 2] < rgbValues[pixel + 1] + marge) && (rgbValues[pixel + 2] > rgbValues[pixel + 1] - marge)) && rgbValues[pixel] < 20)) {
                        output[pixel] = rgbValues[pixel];
                        output[pixel + 1] = rgbValues[pixel + 1];
                        output[pixel + 2] = rgbValues[pixel + 2];
                    }
                }
            }
            System.Runtime.InteropServices.Marshal.Copy(output, 0, ptr1, bytes);
            sourceImage.UnlockBits(bmpdata);
            dest.UnlockBits(bmpdata1);
            return dest;
        }
    }
}
